package com.globallogic.dailyplanner.config;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

public class JettyServer {
	private static final int PORT = 8080;
	private static final String CONTEXT_PATH = "/dailyplanner";
	private static final String HOST = "localhost";
	
	Server jettyServer;

	public void start(){
		jettyServer = new Server(PORT);
		WebAppContext context = new WebAppContext();
		
		context.setResourceBase("target/dailyplanner");
		context.setContextPath(CONTEXT_PATH);
		context.setServer(jettyServer);
		
		context.setConfigurations(new Configuration[]{
				new AnnotationConfiguration(), new WebXmlConfiguration(),
	            new WebInfConfiguration()
		});
		
		jettyServer.setHandler(context);
		jettyServer.setStopAtShutdown(true);
		
		
		try {
			jettyServer.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void stop(){
		try {
			jettyServer.stop();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
public String getURL(String path){
		return String.format("http://%s:%d%s%s", HOST, PORT, CONTEXT_PATH, path);
	}
}
